import moment from 'moment';
import React from 'react';

export const useEchartsData = (info, loadInfo) => {
  const infoData = info?.[loadInfo];
  if (JSON.stringify(info?.[loadInfo]) == '{}' || !info?.[loadInfo]) return [];
  let infoDataList: Record<string, any>[] = [];
  let infoKeys: string[] = Object?.keys(infoData);
  infoKeys?.sort((a: any, b: any) => {
    return moment(a)?.isBefore(moment(b)) ? -1 : 1;
  });
  infoKeys?.forEach((ite) => {
    let totalSum = 0;
    let totalPaid = 0;
    let totalResidue = 0;
    infoData[ite]?.forEach((item) => {
      totalSum += Number(item?.totalSum) || 0;
      totalPaid += Number(item?.totalPaid) || 0;
      totalResidue += Number(item?.totalResidue) || 0;
    });
    const obj = {
      name: ite,
      totalSum,
      totalPaid,
      totalResidue,
    };
    infoDataList?.push(obj);
  });
  return infoDataList;
};
